package com.zhoutao.blog.seven.config;

import com.google.common.collect.Lists;
import com.zhoutao.blog.seven.dao.model.ArticleDao;
import com.zhoutao.blog.seven.dao.model.MenuDao;
import com.zhoutao.blog.seven.dao.repository.ArticleRepository;
import com.zhoutao.blog.seven.dao.repository.MenuRepository;
import com.zhoutao.blog.seven.dao.repository.TagRelationRepository;
import com.zhoutao.blog.seven.model.enums.RefType;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Slf4j
@Configuration
public class ScheduleConfig {

    private final String PUSH = "http://data.zz.baidu.com/urls?site=https://www.zhoutao123.com&token=32trKBucZgw6WyK1";
    @Autowired
    private ArticleRepository articleMapper;
    @Autowired
    private MenuRepository menuMapper;
    @Autowired
    private TagRelationRepository tagRelationRepository;
    @Value("${push.baidu:true}")
    private boolean pushBaidu;

    @Scheduled(cron = "0 0 * * * ?")
    @SneakyThrows
    public void pushBaidu() {
        if (!pushBaidu) {
            return;
        }

        // 查询文章
        List<String> links = Lists.newArrayList();
        for (ArticleDao articleDao : articleMapper.list()) {
            Long id = articleDao.getId();
            String link = "https://www.zhoutao123.com/blog/" + id;
            links.add(link);
            PostUrl.baiduPost(PUSH, link);
            Thread.sleep(1_000);
        }

        // 查询目录
        for (MenuDao menuDao : menuMapper.all()) {
            String url = menuDao.getUrl();
            String link = "https://www.zhoutao123.com" + url;
            links.add(link);
            PostUrl.baiduPost(PUSH, link);
            Thread.sleep(1_000);
        }

        log.info("推送SEO完成,推送URL,count={},detail={}", links.size(), links);
    }


    @Scheduled(cron = "0 30 * * * ?")
    @SneakyThrows
    public void updateTag() {
        log.info("开始定时更新文章标签信息");
        tagRelationRepository.cleanAll();
        List<ArticleDao> list = articleMapper.list();
        for (ArticleDao articleDao : list) {
            List<String> keys = findTags(articleDao.getKeys());
            if (CollectionUtils.isEmpty(keys)) {
                continue;
            }
            tagRelationRepository.updateTagRelation(articleDao.getId(), RefType.ARTICLE, keys);
        }
    }

    public List<String> findTags(String keys) {
        if (keys == null || keys.trim().length() == 0) {
            return Collections.emptyList();
        }
        String[] tags = keys.split(";");
        for (int i = 0; i < tags.length; i++) {
            String tag = tags[i];
            if (tag == null || tag.trim().length() == 0) {
                continue;
            }
            tags[i] = tag.trim();
        }
        return Arrays.asList(tags);
    }
}
